14. Solution: Mockito Verify
Solution: Mockito Verify
ND079 JPND C3 L5 A09 Mockito Verify Exercise Solution
ChargeServiceTest.java
@ExtendWith(MockitoExtension.class)
class ChargeServiceTest {
private ChargeService chargeService;
@Mock
private ChargeUserApiHttpClient chargeUserApiHttpClient;
@BeforeEach
void init() {
chargeService = new ChargeService(chargeUserApiHttpClient);
}
@Test
void chargeUser() {
chargeService.chargeUser(
"UserId123",
List.of(
new TicketItem("Soda", new BigDecimal("10.0"), new BigDecimal("1.0")), // 11.00
new TicketItem("Soda", new BigDecimal("12.0"), new BigDecimal("1.2")) // 13.20
),
new BigDecimal("1.50"), //+ 1.50
new BigDecimal("0.25") //- 0.25
);
verify(chargeUserApiHttpClient, times(1)).charge(eq("UserId123"), eq(new BigDecimal("25.45")));
}
}